<template>
  <div>
    <el-dialog title="操作确认" :visible.sync="dialogVisible" lock-scroll width="400px">
      <div class="dialog_div_new">
        <div class="dialog_middle">
          <i class="el-icon-warning"></i>
          <div>
            <p>您的“反审”功能还未开通， <br />需要一键开通“反审”功能吗？</p>
          </div>
        </div>
        <div class="dialog_middle_button">
            <button @click="onChange(index)" :class="item.state ? 'select_button' : ''"
              v-for="(item, index) in buttonState" :key="index">
              {{ item.name }}
            </button>
        </div>
        <div class="dialog_bottom">
          <el-button v-if="freeCount>0" type="primary" class="free" @click.stop="onConfirmFree" :loading="freeCountLoading">
            免费体验(剩{{freeCount}}次)
          </el-button>
          <el-button  type="primary" @click.stop="onConfirm">支付</el-button>
          <el-button @click.stop="dialogVisible = false">取消</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import Vue from 'vue'
import { USER_INFO, USER_NAME, ACCOUNT_BOOK } from '@/store/mutation-types'
import { postAction, getAction } from '@/api/manage'
export default {
  data() {
    return {
      account: null,
      dialogVisible: false,
      freeCount: 0, // 剩余免费次数
      freeCountLoading: false,
      buttonState: [
        {
          name: '9元/次',
          price: 9,
          state: true,
        },
        {
          name: '90元/年',
          price: 90,
          state: false,
        },
      ],
      current: 0,
      list: [],
      selectPayment: {

      }
    }
  },
  watch: {
    dialogVisible(val) {
      if (val == true) {
        this.getGoodsInfo()
        this.getFreeCountApi()
      }
    },
  },
  methods: {
    async getFreeCountApi() { // 获取免费次数
      this.account = this.$ls.get('Account_Book')
      if(!this.account) return
      let res = await getAction('/xghBookAntiReviewNumber/queryByAccountBookId', {
        accountBookId: this.account.id
      }) || {}
      if(res.success) this.freeCount = res.result.surplusAntiReviewNumber
    },
    async onConfirmFree() { // 点击了免费体验
      this.freeCountLoading = true
      let res = await getAction('/xghBookAntiReviewNumber/addByAccountBookId', {
        accountBookId: this.account.id
      }) || {}
      this.freeCountLoading = false
      if(res.success) {
        this.$emit('onConfirmFree', 'onUnCheck')
        this.dialogVisible = false
      } else {
        this.$message.error(res.message || '使用免费次数出错!')
      }
    },
    getGoodsInfo() {
      getAction('/XghAuthorizationCenter/xghAuthorizationCenterUserPrice/list').then(res => {
        if (res.code == 200) {
          let list = res.result.records || []
          list.forEach(item => {
            if (item.goodsType == 3) {
              this.list[0] = item
              this.buttonState[0].name = item.price + '元/次'
              this.buttonState[0].price = item.price
            } else if (item.goodsType == 4) {
              this.buttonState[1].name = item.price + '元/年'
              this.buttonState[1].price = item.price
              this.list[1] = item
            }
          })
        }
      })
    },
    // 选择支付
    onChange(index) {
      this.current = index
      this.buttonState.forEach((item, i) => {
        if (index == i) {
          item.state = true
          this.selectPayment = item
        } else {
          item.state = false
        }
      })
    },
    onConfirm() {
      let accountBook = Vue.ls.get(ACCOUNT_BOOK)
      let accountId = accountBook && accountBook.id
      let params = {
        goodsId: this.list[this.current].id,
        goodsName: this.list[this.current].name,
        goodsType: this.list[this.current].goodsType,
        xghAccountBookId: accountId,
        orderMoney: this.list[this.current].price,
        // orderMoney: 0.01,
      }
      postAction('/XghAuthorizationCenter/xghAuthorizationCenterOrders/add', params).then(res => {
        if (res.code == 200) {
          this.$router.push({
            path: '/setting/buy/confirmPay',
            query: {
              id: res.result.id
            }
          })
        } else {
          this.$elMessage.error(res.message)
        }
      })
    },
  },
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog {
  margin-top: 33vh !important;
  overflow: hidden;

  .el-dialog__body {
    padding: 0;
    height: 184px;
  }

  .el-dialog__header {
    width: 100%;
    height: 50px;
    box-sizing: border-box;
    padding: 0 0.375023rem;
    border-bottom: 1px solid #eeeeee;
    display: flex;
    align-items: center;
    justify-content: space-between;

    span {
      font-size: 0.300019rem;
      color: #666;
      font-weight: 600;
    }

    .el-dialog__headerbtn {
      position: static;

      .el-icon-close {
        color: #999;
        font-size: 0.48753rem;
      }
    }
  }
}

.el-icon-warning {
  width: 42.33px;
  height: 41.89px;
  margin-top: 11px;
  color: #fd5050;
  font-size: 0.7rem;
}

.dialog_div_new {
  width: 100%;
  height: 184px;
  overflow: auto;
  background: #fff;

  .dialog_middle {
    width: 100%;
    box-sizing: border-box;
    padding: 15px 70px 7px 51px;
    display: flex;
    align-items: center;

    .svg_icon {
      width: 0.787549rem;
      height: 0.787549rem;
      margin-top: -0.9rem;
    }

    div {
      margin-left: 26px;
      box-sizing: border-box;

      p {
        font-size: 14px;
        font-family: PingFang SC;
        font-weight: 400;
        line-height: 24px;
        color: #666666;
        margin: 0;
      }
    }
  }
  .dialog_middle_button {
    width: 100%;
    display: flex;
    justify-content: center;

    button {
      width: 80px;
      height: 30px;
      border-radius: 5px;
      font-size: 14px;
      font-family: PingFang SC;
      font-weight: 400;
      border: 1px solid #dddddd;
      background: #ededed;
      color: #666;

      &:last-child {
        margin-left: 10px;
      }
    }

    .select_button {
      background: #fd6c21;
      border-color: #fd6c21;
      color: #fff;
    }
  }

  .dialog_bottom {
    width: 100%;
    display: flex;
    justify-content: center;
    margin-top: 21px;
    .free {
      background-color: #009405 !important;
      border-color: #009405 !important;
      width: 130px !important;
    }

    ::v-deep .el-button {
      width: 60px;
      height: 30px;
      text-align: center;
      box-sizing: border-box;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 14px;
      border-radius: 5px;
      background: #fff;
      border: 1px solid #dddddd;
      color: #666666;
    }

    ::v-deep .el-button--primary {
      background-color: #1890ff;
      border-color: #1890ff;
      color: #fff;
    }

    ::v-deep .el-button--default {
      margin-left: 18px;
    }
  }
}</style>